<?php
namespace Home\Controller;
use Think\Controller;

class ReplyController extends CommonController {
    public function index(){

        if($_POST){
            $keyword = I('post.keyword');
            $ret     = M();
            $data    = $ret->query("select a.id,a.eventkey,b.text from sae_wx_event_data as a inner join sae_wx_all_text as b on a.id=b.eventid and a.datatype='text' where a.eventkey like '{$keyword}%'");
            if( !empty($data) ){
                $this->assign('data', $data );
            } else {   
                $this->assign('reminder','无此关键字请重新查询！');
            }
        } else {
            $data  = M('wx_event_data');
        
            $count = $data->where("datatype = 'text'")->count();
            $page  = new \Think\Page($count,C("PAGESIZE"));
            $page->setConfig('prev','上一页');
            $page->setConfig('next','下一页');
            $list = $data->query("select a.id,a.eventkey,b.text from sae_wx_event_data as a inner join sae_wx_all_text as b on a.id =b.eventid where datatype = 'text' order by a.eventkey asc limit {$page->firstRow},{$page->listRows}");
        
            $show  = $page->show();
            
            $this->assign( "list", $list);
            $this->assign( "page", $show);
        }

        $this->display();
    
    }
   
    public function deletes(){
        
    	$_id = I('post.id');
        $ret = M();
        $ret->execute("delete a,b from sae_wx_all_text as a,sae_wx_event_data as b where b.id='{$_id}' and a.eventid ='{$_id}'");

    }
   
    public function inserts(){
    	
    	if($_POST){
    		
			$data['eventkey'] = I('post.eventkey');
	    	$data['text']     = I('post.text');
            if(!empty($data['eventkey']) && !empty($data['text'])){
		    	$inst = M();
		    	$inst->execute("insert into sae_wx_event_data (weixinhao, eventkey, datatype) values('gh_8eefc37aa888','{$data['eventkey']}','text')");
                $inst = M("wx_event_data");
                $ret = $inst->query("select LAST_INSERT_ID()");
                $data['eventid'] = $ret[0]["last_insert_id()"];
                $insrt = M("wx_all_text");
                $insrt->where("eventid = {$data['eventid']}")->add($data);
                
		    	$this->success('添加成功', '',0);
    		} else {
    			$this->error('数据不能为空,添加失败！', '',0);
    		}			
    	}
    
    }
    
    public function updates(){

    	if($_POST){
    		
    		$data['eventkey']  = I('post.eventkey');
    		$data['text']      = I('post.text');
            if(!empty($data['eventkey']) && !empty($data['text'])){
                
                $data['id'] = I('post.id');
	    		$upt = M();
                $upt->execute("update sae_wx_event_data set weixinhao='gh_8eefc37aa888', eventkey=
                    '{$data['eventkey']}', datatype='text' where id = '{$data['id']}'");
                $upt->execute("update sae_wx_all_text set eventid={$data['id']}, text=
                    '{$data['text']}'  where eventid = '{$data['id']}'");
    		   
                $this->success('修改成功', '',0);
            } else {
                $this->error('数据不能为空,编辑失败！', '',0);
            }
    	
    	}
    
    }
}